﻿using UnityEngine;
using System.Text;

public static class SerializeHelper
{
    #region Vector 3

    /// <summary>
    /// 将Vector3添加到StringBuilder中
    /// <para>添加格式为：x,y,z</para>
    /// </summary>
    /// <param name="stringBuilder">有stringBuilder则将序列化到此中</param>
    /// <param name="v3"></param>
    public static void ToSerializedString(Vector3 v3, StringBuilder stringBuilder)
    {
        //每个保留小数点后3位
        stringBuilder.AppendFormat("{0:F3},{1:F3},{2:F3}", v3.x, v3.y, v3.z);
        
    }

    /// <summary>
    /// 将Vector3序列化xyz部分
    /// <para>添加格式为：x,y,z</para>
    /// </summary>
    /// <param name="v3"></param>
    public static string ToSerializedString(Vector3 v3)
    {
        //每个保留小数点后3位
        return string.Format("{0:F3},{1:F3},{2:F3}", v3.x, v3.y, v3.z);
    }

    /// <summary>
    /// 从String中获得Vector3对象
    /// </summary>
    /// <param name="v"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    public static Vector3 GetVector3FromString(string str)
    {
        Vector3 v3 = Vector3.zero;
        string[] v3Pos = str.Split(',');
        float.TryParse(v3Pos[0], out v3.x);
        float.TryParse(v3Pos[1], out v3.y);
        float.TryParse(v3Pos[2], out v3.z);
        return v3;
    }
    #endregion

    #region 颜色
    /// <summary>
    /// 将颜色序列化
    /// </summary>
    /// <param name="col"></param>
    /// <returns></returns>
    public static string ToSerializedString(Color col, char spliter)
    {
        return string.Format("{1:F3}{0}{2:F3}{0}{3:F3}{0}{4:F3}", spliter, col.r, col.g, col.b, col.a);
    }

    /// <summary>
    /// 从字符串中反序列化出颜色
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static Color GetColorFromString(string str,char spliter)
    {
        Color col = Color.black;
        string[] colStr = str.Split(spliter);
        float.TryParse(colStr[0], out col.r);
        float.TryParse(colStr[1], out col.g);
        float.TryParse(colStr[2], out col.b);
        float.TryParse(colStr[3], out col.a);

        return col;

    }

    #endregion
}


/// <summary>
/// 对Vector3的辅助
/// </summary>
public static class VectorHelper
{
    /// <summary>
    /// 将Vector3添加到StringBuilder中
    /// <para>添加格式为：x,y,z</para>
    /// </summary>
    /// <param name="stringBuilder">有stringBuilder则将序列化到此中</param>
    /// <param name="v3"></param>
    [System.Obsolete("Use SerializeHelper.ToSerializedString instead")]
    public static void ToSerializedString(Vector3 v3, StringBuilder stringBuilder)
    {
        //--可考虑这里加入最大长度保留N位小数，以及直接0,0,0的情况处理（似乎没必要？）
        stringBuilder.Append(v3.x);
        stringBuilder.Append(',');
        stringBuilder.Append(v3.y);
        stringBuilder.Append(',');
        stringBuilder.Append(v3.z);
    }

    /// <summary>
    /// 将Vector3添加到StringBuilder中
    /// <para>添加格式为：x,y,z</para>
    /// </summary>
    /// <param name="v3"></param>
    [System.Obsolete("Use SerializeHelper.ToSerializedString instead")]
    public static string ToSerializedString(Vector3 v3)
    {
        return string.Format("{0},{1},{2}", v3.x, v3.y, v3.z);
    }

    /// <summary>
    /// 从String中获得Vector3对象
    /// </summary>
    /// <param name="v"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    [System.Obsolete("Use SerializeHelper.GetVector3FromString instead")]
    public static Vector3 GetVector3FromString(string str)
    {
        Vector3 v3 = Vector3.zero;
        string[] v3Pos = str.Split(',');
        float.TryParse(v3Pos[0], out v3.x);
        float.TryParse(v3Pos[1], out v3.y);
        float.TryParse(v3Pos[2], out v3.z);
        return v3;
    }

    public static Vector3 RandomVector3(float range)
    {
        Vector3 pos = new Vector3();
        pos.x = Random.Range(-range, range);
        pos.y = Random.Range(-range, range);
        pos.z = Random.Range(-range, range);
        return pos;
    }
}